#include "pthread.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define count 5000

// static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t mutex;

void *sum(void *arg){
    pthread_mutex_lock(&mutex);
    int *result = arg;
    *result = *result +1;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_mutex_init(&mutex,PTHREAD_MUTEX_TIMED_NP);
    pthread_t thread[count];
    int num=0;
    for (int i = 0; i < count; i++)
    {
        pthread_create(&thread[i],NULL,sum,&num);
    }
    
    for (size_t i = 0; i < count; i++)
    {
        pthread_join(thread[i],NULL);
    }
    sleep(1);
    printf("num = %d",num);

    return 0;
}
